<?php

/***
 * Class WeixinDrawcashHistoryModel
 * 提现记录，包括支付宝和微信
 */
class WeixinDrawcashHistoryModel extends PdoModel
{
    protected $_name = 'weixin_drawcash_history';
    const PERDAY_WXWITHDRAW_KEY = 'perday_wxwithdraw_';
    const DEALDRAW_WXWITHDRAW_KEY = 'dealdraw_wxwithdraw_';
    const WXWITHDRAW_FREQUENCY_KEY = 'wxwithdraw_frequency_';       #提现频率限制60s一次
    const WXWITHDRAW_FREQUENCY_TIME = 60;

    function __construct()
    {
        parent::__construct();
    }


    function addData($info){
        if (empty($info)){
            return false;
        }

        return $this->insert($info);
    }

    function getDataById($id){
        if(empty($id)){
            return false;
        }

        $sql = "select * from {$this->_name} where id = {$id}";
        $data = $this->fetchRow($sql);

        return $data;
    }

    function getListData($page = 1,$page_size =  20,$condition = array()){
        $sql = " select a.* from {$this->_name} a, user b where 1 and a.uid=b.uid and b.frozen=0 and b.ban=0 ";
        if(isset($condition['status'])) {
            $sql .= " and  a.status={$condition['status']} ";
        }
        if(!empty($condition['uid'])) {
            $sql .= " and  a.uid={$condition['uid']} ";
        }
        if(!empty($condition['type'])) {
            $sql .= " and  a.type='{$condition['type']}' ";
        }
        if(!empty($condition['realname'])) {
            $sql .= " and  a.realname='{$condition['realname']}' ";
        }
        if(!empty($condition['start_date'])) {
            $sql .= " and  a.created_at>={$condition['start_date']} ";
        }
        if(!empty($condition['end_date'])) {
            $sql .= " and  a.created_at<{$condition['end_date']} ";
        }
        $sql .= " order by  a.realname asc ";
        $start = ($page -1 ) * $page_size;
        $sql .= " limit {$start}, {$page_size}";
        try{
            $data = $this->fetchAll($sql);
        }catch(Exception $ex){
            $data = array();
        }
        return $data;
    }
    function getListCount($condition = array()){
        $sql = " select count(*) as num,sum(a.credits)/100 as total from {$this->_name}  a, user b where 1 and a.uid=b.uid and b.frozen=0 and b.ban=0  ";
        if(isset($condition['status'])&& $condition['status'] != 'all') {
            $sql .= " and a.status={$condition['status']} ";
        }
        if(!empty($condition['uid'])) {
            $sql .= " and a.uid={$condition['uid']} ";
        }
        if(!empty($condition['type'])) {
            $sql .= " and a.type='{$condition['type']}' ";
        }
        if(!empty($condition['realname'])) {
            $sql .= " and a.realname='{$condition['realname']}' ";
        }
        if(!empty($condition['start_date'])) {
            $sql .= " and a.created_at>={$condition['start_date']} ";
        }
        if(!empty($condition['end_date'])) {
            $sql .= " and a.created_at<{$condition['end_date']} ";
        }
        $result = $this->fetchRow($sql);
        if(empty($result)) {
            $result = array();
        }
        return $result;
    }
}